<?php

abstract class Quad_View_Helper_Html {

	public static function extractKeywords($str) {
		$words = array();
		foreach (explode(' ', $str) as $word) {
			if (strlen($word) >= 6) {
				$words[] = $word;
			}
		}
		return join(', ', $words);
	}
	
  /**
   * Build a select-one tag with all it's options
   *
   * @param string $name  The name of the select
   * @param array $valuesHash  An array of value => label options
   * @param string $selected  The option to preselect
   * @param array $attributes  Additional attributes to add to the select tag
   * @return string
   */
  public static function select($name, $valuesHash, $selected = null, $attributes = array()) {
    $out = '';
    $out .= '<select name="'. $name . '"' . self::_tagAttributes($attributes) . '>' . "\n";
    $out .= self::options($valuesHash, $selected);
    $out .= '</select>';
    return $out;
  }

  /**
   * Build a options to go inside a select-one
   *
   * @param array $valuesHash  An array of value => label options
   * @param string $selected  The option to preselect
   * @return string
   */
   public static function options($valuesHash, $selected = null) {
    if (is_array($selected)) {
      foreach ($selected as &$s) $s = (string) $s;
    }
    $out = '';
    foreach ($valuesHash as $value => $name) {
      if (is_array($selected)) {
        $sel = in_array((string)$value, $selected, true) ? ' selected="selected"' : '';
      } else {
        $sel = $selected !== null && (string)$selected === (string)$value ? ' selected="selected"' : '';
      }
      $out .= '<option value="' . esc($value) . '"' . $sel . '>' . esc($name) . '</option>' . "\n";
    }
    return $out;
  }
  
  /**
   * Build a list of radios
   *
   * @param string $name  The name of the radio group
   * @param array $valuesHash  An array of value => label options
   * @param string $checked  The option to preselect
   * @param string $separator  The string to insert between each radio
   * @param array $attributes  Additional attributes to add to the select tag
   * @return string
   */ 
  public static function radios($name, $valuesHash, $checked = null, $separator = '<br />', $attributes = array()) {
    $out = '';
    foreach ($valuesHash as $value => $name) {
      $sel = $selected !== null && (string)$selected === (string)$value ? ' selected="selected"' : '';
      $out .= '<input type="radio" name="' . $name . '" value="' . esc($value) . '"' .
        $sel . self::_tagAttributes($attributes). '><label for="' . $name . '" class="label">' . 
        esc($name) . $separator . "</label>\n"
      ;
    }
    return $out;  
  }
  
  /**
   * Build a list of checkboxes
   *
   * @param string $name  The name of the checkbox group
   * @param array $valuesHash  An array of value => label options
   * @param string $checked  The option(s) to preselect
   * @param string $separator  The string to insert between each radio
   * @param array $attributes  Additional attributes to add to the select tag
   * @return string
   */   
  public static function checkboxes($name, $valuesHash, $checked = null, $separator = '<br />', $attributes = array()) {
    $out = '';
    foreach ($valuesHash as $value => $name) {
      $chk = $checked !== null && in_array((string)$value, $checked) ? ' checked="checked"' : '';
      $out .= '<input type="checkbox" name="' . $name . '" value="' . esc($value) . '"' .
        $chk . self::_tagAttributes($attributes). '><label for="' . $name . '" class="label">' . 
        esc($name) . $separator . "</label>\n"
      ;
    }
    return $out;    
  }
  
  /**
   * Build a list of tr tags with a header row if desired
   *
   * @param array $recordset
   * @param bool $useHeader  If true, use array keys of first record to generate labels for table columns
   * @return string
   */   
  public static function trs($recordset, $useHeader = true) {
    $out = '';
    if ($useHeader) {
      $out .= "<tr>\n";
      foreach ($recordset[0] as $fieldName => $value) {
        $out .= "<th>" . esc($fieldName) . "</th>\n";
      }
      $out .= "</tr>\n";
    }
    foreach ($recordset as $record) {
      $out .= "<tr>\n";
      foreach ($record as $value) {
        $out .= "<td>" . esc($value) . "</td>\n";
      }
      $out .= "</tr>\n";
    }
    return $out;
  }
  
  /**
   * Helper function to convert array of attribute => value pairs to html
   *
   * @param array $attributeValues
   * @return string
   */   
  protected static function _tagAttributes($attributeValues = array()) {
    $out = '';
    foreach ($attributeValues as $attr => $value) {
      $out .= " $attr=\"" . esc($value) . "\"";
    }
    return $out;
  }
  
  
  /**
   * Output html for a results pager
   *
   * @param int $page
   * @param int $numPages
   * @param string $label
   * @return string
   */     
  public function pager($page, $numPages, $label) {
    $pageArray = array();
    $isLastPage = false;
    for ($i = 1; $i < $numPages + 1; ++$i) {
      // we want page buttons for the first page, the last page, and the middle 9 pages
      if (
        ($page <= 10 && ($i<=10 || $i>$numPages-1)) ||
        ($page > 10 && $numPages > $page+9 && ($i<2 || ($i>=$page-4&&$i<=$page+4) || $i>$numPages-1 )) ||
        ($page > 10 && $numPages <= $page+9 && ($i<2 || $i>=$numPages-9))
      ) {
        $pageArray[] = $i;
        $isLastPage = true;
      } else {
        if ($isLastPage) {
          $pageArray[] = 0;
        }
        $isLastPage = false;
      }
    }
    
    $html = '';
    $html .= '<div class="pager">';
    $html .= "<span class=\"label\">$label</span>";
    if ($numPages > 1) {
      if ($page == 1) {
        $html .= '<span class="disabled">&lt; Prev</span>';
      } else {
        $html .= '<input type="submit" name="page" value="&lt; Prev" class="prev" />';
      }
      foreach ($pageArray as $i) {
        if ($page == 0) {
          $html .= '<span class="elipses">&#133;</span>';
        } elseif ($page == $i) {
          $html .= '<span class="current">' . $i . '</span>';
        } else {
          $html .= '<input type="submit" name="page" value="' . $i . '" />';
        }
      }
      if ($page == $numPages) {
        $html .= '<span class="disabled">Next &gt;</span>';
      } else {
        $html .= '<input type="submit" name="page" value="Next &gt;" class="next" />';
      }
    }
    $html .= '</div>';
    return $html;
  }
    
}

